package com.kfm.map;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class MapDemo {

    // aaabbbsssddcc ==> a=3 b=3 d 2 c 2 s 3
    // username ==> pomelo  password ==> 123456
    // key-value ==> Map<K, V>
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4);
        Map<String, Integer> map = Map.of("a", 3, "b", 3, "c", 2);
        // Integer d = map.put("d", 2);
        // 1. 通过键找值 keySet 获取键的集合 get(key) 获取键对应的值
        System.out.println(map);
        Set<String> strings = map.keySet();
        for (String s : strings) {
            Integer value = map.get(s);
            System.out.println(s + "==>" + value);
        }
        // 2. Entry 对象里面存储的是 键和值 getKey() getValue()
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        // 3. forEach()
        // list.forEach(new Consumer<Integer>() {
        //     @Override
        //     public void accept(Integer integer) {
        //
        //     }
        // });
        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println(s + "=" + integer);
            }
        });
        map.forEach((a, b) -> System.out.println(a + "=" + b));
    }
}
